package com.dk.demo.core.service.strategy;

import com.dk.demo.common.model.strategy.StrategyType;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cy
 * @since 2022/8/1 14:13
 */
@Component
public class StrategyFactory implements InitializingBean {

    private static Map<StrategyType, AbstractStrategy> strategyMap = new HashMap<>();

    @Resource
    ApplicationContext application;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, AbstractStrategy> beansOfType = application.getBeansOfType(AbstractStrategy.class);
        for (AbstractStrategy value : beansOfType.values()) {
            strategyMap.put(value.type(), value);
        }
    }

    public static AbstractStrategy getStrategy(StrategyType strategyType) {
        return strategyMap.get(strategyType);
    }
}
